﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace School_Management
{
    public partial class AddPerson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strcon = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                SqlConnection con = new SqlConnection(strcon);
                con.Open();

                string cmdtext = "select * from Groups";
                SqlCommand cmd = new SqlCommand(cmdtext, con);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    ListItem item = new ListItem();
                    item.Value = rdr["Id"].ToString();
                    item.Text = rdr["GroupName"].ToString();
                    ddlGroup.Items.Add(item);
                 
                }
                con.Close();

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int UserId = Convert.ToInt32(Session["User_Id"]);
            int Group = Convert.ToInt32(ddlGroup.SelectedValue);
            string name = txtName.Text;
            string gender = chkGender.SelectedValue;
            int age = Convert.ToInt32(txtAge.Text);
            string address = txtAddress.InnerText;
            int phone = (txtPhone.Text.Trim() != "") ? Convert.ToInt32(txtPhone.Text) : -1;
            string filename = "";

            if (FileUpload1.PostedFile != null)
            {
                filename = FileUpload1.PostedFile.FileName;
                FileUpload1.SaveAs(Server.MapPath("~/Images/" + filename));
            }
            string strcon = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            con.Open();

            string cmdtext = "insert  into PersonDetails(User_Id,Group_Id,Name,Gender,Age,Address,PhoneNo,Photo) values(@user,@group,@name,@gender,@age,@address,@phoneno,@photo)";
            SqlCommand cmd = new SqlCommand(cmdtext,con);

            cmd.Parameters.AddWithValue("@user", UserId);
            cmd.Parameters.AddWithValue("@group", Group);
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@gender", gender);
            cmd.Parameters.AddWithValue("@age", age);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@phoneno", phone);
            cmd.Parameters.AddWithValue("@photo", filename);

            int retvalue = cmd.ExecuteNonQuery();
            con.Close();

            if (retvalue > 0)
                Response.Redirect("UserInfo.aspx");
        }
    }
}